package com.jiechuang.user.controller;


import com.github.pagehelper.PageInfo;
import com.jiechuang.common.annotation.JWTPermission;
import com.jiechuang.common.bean.User;
import com.jiechuang.common.bean.addOrUpdate.UserAddOrUpdate;
import com.jiechuang.common.web.Code;
import com.jiechuang.common.web.Response;
import com.jiechuang.user.dto.UserQueryInfo;
import com.jiechuang.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/list")
    @JWTPermission(role = "user")
    public Response userList(@RequestBody UserQueryInfo userQueryInfo){
        long l = System.currentTimeMillis();
        System.out.println(l);
        try {
            PageInfo<User> pageInfo = userService.getUserList(userQueryInfo);
            long l2 = System.currentTimeMillis();
            System.out.println(l2-l);
            return new Response(Code.SUCCESS,pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }

    }
    @PostMapping("/user")
    @JWTPermission(role = "user")
    public Response addUser(@RequestBody UserAddOrUpdate userAddOrUpdate){
        try {
             userService.addUser(userAddOrUpdate);
            return new Response(Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SAVE_ERROR);
        }
    }
    @DeleteMapping("/{id}")
    @JWTPermission(role = "user")
    public Response deleteUser(@PathVariable Integer id){
        try {
            userService.deleteUser(id);
            return new Response(Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }
    @PutMapping("/user")
    @JWTPermission(role = "user")
    public Response updateUser(@RequestBody UserAddOrUpdate userAddOrUpdate){
        try {
            userService.updateUser(userAddOrUpdate);
            return new Response(Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }
    @GetMapping("/{id}")
    @JWTPermission(role = "user")
    public Response getUser(@PathVariable Integer id){
        try {
            User user=userService.getUser(id);
            return new Response(Code.SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }
    @PutMapping ("/{id}")
    @JWTPermission(role = "user")
    public Response updateUserStatus(@PathVariable Integer id){
        try {
            userService.updateUserStatus(id);
            return new Response(Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }
    @PostMapping (path = {"/{userId}/roles/{roleIds}","/{userId}/roles/"})
    @JWTPermission(role = "user")
    public Response giveRole(@PathVariable Integer userId,@PathVariable(required = false) List<Integer> roleIds){
        try {
            userService.giveRole(userId,roleIds);
            return new Response(Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(Code.SEARCH_ERROR);
        }
    }

}
